package view.map;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;

public class HexTileMapper implements LayoutManager, java.io.Serializable
{ private static final long serialVersionUID = -858342723067286796L;
  int rows, cols;

  public HexTileMapper(int row, int col) 
  { if ((row == 0) && (col == 0)) 
    { throw new IllegalArgumentException("rows and cols cannot both be zero"); }
    this.rows = row; this.cols = col;
  }
  public int getRows() { return rows; }
  public int getColumns() { return cols; }
  public void setRows(int row) 
  { if ((row == 0) && (this.cols == 0)) 
    { throw new IllegalArgumentException("rows and cols cannot both be zero"); }
    this.rows = row;
  }
  public void setColumns(int col) 
  { if ((col == 0) && (this.rows == 0)) 
    { throw new IllegalArgumentException("rows and cols cannot both be zero"); }
    this.cols = col;
  }
  public void addLayoutComponent(String name, Component comp) { /* Blank out */ }
  public void removeLayoutComponent(Component comp) { /* Blank out */ }
  
  public Dimension preferredLayoutSize(Container parent)
  { synchronized ( parent.getTreeLock() )
    { return new Dimension((rows + 1) * 100, (cols + 1) * 117); }
  }
  public Dimension minimumLayoutSize(Container parent)
  { synchronized ( parent.getTreeLock() )
    { return new Dimension((rows + 1)* 100, (cols + 1)* 117); }
  }
  public void layoutContainer(Container parent)
  { synchronized (parent.getTreeLock())
    { int ncomponents = parent.getComponentCount();
      int nrows = rows;
      int ncols = cols;
      if ( ncomponents == 0 )
      { return; }
      
      boolean staggeredRow = false;
      int compIndex = 0;
      for (int i = 0;i < ncols;i++)
      { for(int j = 0;j < nrows;j++)
        { 
    	  if(staggeredRow)
          { 
        	parent.getComponent(compIndex).setBounds(i * 100, (j * 117) + 58, 135, 117); 
          }
          else
          { 
        	  parent.getComponent(compIndex).setBounds(i * 100, j * 117, 135, 117); 
          }
          compIndex++;
        } staggeredRow = !staggeredRow;
      }
    }
  }
}